﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace pulsarc
{
    class Program
    {
        /// <summary>
        /// If we pass a file, compile it and nothing else.
        /// If the args[0] is null, we will scan all the files in the root
        /// for .pul and compile them down.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            StringBuilder Code = new StringBuilder();
            //Make sure the args at least
            //2 chars.
            if (args.Length > 0)
            {
                //Ok this means theres one file technicallly.
                System.IO.StreamReader io = new StreamReader(args[0]);
                Code.Append(io.ReadToEnd());
                //Close the io.
                io.Close();
            }
            else
            {
                //Ok now here we will scan the root directory of
                //where the compiler is stored for all of its code
                //files
                string[] files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.pl", SearchOption.TopDirectoryOnly);
                //Now read each file.
                for (int i = 0; i < files.Length; i++)
                {
                    Console.WriteLine("Reading File: " + files[i]);
                    System.IO.StreamReader io = new StreamReader(files[i]);
                    Code.Append(io.ReadToEnd() + "\n");
                    io.Close();
                }
                //Thats it for that.
            }
            if (Code.Length < 2)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(DateTime.Now.ToString() + " No code found to compile!");
                Console.ReadLine();
                return;
            }
            //Now compile down all the code.
            Language.Compiler comp = new Language.Compiler(Code.ToString());
            string s = comp.Compile();
            StreamWriter sw = new StreamWriter("output.asm");
            sw.Write(s);
            sw.Close();
            Console.ReadLine();
        }
        
    }
   
}
